﻿using UnityEngine;

namespace BToolkit
{
    [RequireComponent(typeof(MediaPlayerCtrl))]
    public class ARVideoPlayer_EasyMT : MonoBehaviour
    {
        MediaPlayerCtrl mediaPlayerCtrl;
        MeshRenderer meshRenderer;
        float videoW, videoH;

        void Awake()
        {
            mediaPlayerCtrl = GetComponent<MediaPlayerCtrl>();
            meshRenderer = GetComponent<MeshRenderer>();
            meshRenderer.enabled = false;
        }

        void Update()
        {
            if (!meshRenderer.enabled)
            {
                if (mediaPlayerCtrl.GetCurrentState() == MediaPlayerCtrl.MEDIAPLAYER_STATE.PLAYING)
                {
                    videoW = mediaPlayerCtrl.GetVideoWidth();
                    videoH = mediaPlayerCtrl.GetVideoHeight();
                    transform.parent.GetComponent<ARVideoPlayer>().OnGetedVideoSize(videoW, videoH);
                    meshRenderer.enabled = true;
                }
            }
        }

        public void Play(string videoUrl)
        {
            meshRenderer.enabled = false;
            mediaPlayerCtrl.GetComponent<MeshRenderer>().enabled = false;
            mediaPlayerCtrl.m_strFileName = "file://" + videoUrl;
            mediaPlayerCtrl.Play();
        }
    }
}
